﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using Quartz;
using Quartz.Impl;
using Common.Logging;
using Quartz.Impl.Calendar;

namespace QuartzForzzl
{
    public class WriteLogScheduler_Xml
    {

        static WriteLogScheduler_Xml _instance = null;
        static object lockObj = new object();
        static NameValueCollection properties = new NameValueCollection();
        static ISchedulerFactory _sf = null;
        static IScheduler _sched = null;
        static WriteLogScheduler_Xml()
        {
            properties["quartz.scheduler.instanceName"] = "XmlConfiguredInstance";
            // set thread pool info
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "5";
            properties["quartz.threadPool.threadPriority"] = "Normal";
            // job initialization plugin handles our xml reading, without it defaults are used
            properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz";
            properties["quartz.plugin.xml.fileNames"] = "~/quartz_jobs.xml";
            _sf = new StdSchedulerFactory(properties);
            _sched = _sf.GetScheduler();
        }
        public static WriteLogScheduler_Xml Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObj)
                    {
                        if (_instance == null)
                        {
                            _instance = new WriteLogScheduler_Xml();
                        }
                    }
                }
                return _instance;
            }
        }

        public void Start()
        {

            ILog log = LogManager.GetLogger(typeof(WriteLogScheduler_Xml));
            // computer a time that is on the next round minute
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            var dailyCalendar = new DailyCalendar("00:01", "23:59");
            dailyCalendar.InvertTimeRange = true;
            _sched.AddCalendar("cal1", dailyCalendar, false, false);//使用XML时必须有这行
            _sched.Start();

        }
        public void Stop()
        {
            _sched.Shutdown(true);
        }

    }
}